<?php if (!defined("_ECRIRE_INC_VERSION")) return;

/**
 * Price calculation for one product
 * 
 */
 
 if (!function_exists('ppr'))
 {
     function ppr($var)
     {
         echo '<pre>';
         print_r($var);
         echo '</pre>';
    
     }
 }
 
 if (!function_exists('ppre'))
 {
     function ppre($var)
     {
         ppr($var);
         exit;
     }
 }
 
 
 
 
function get_discount_action($id_article)
{
	require_once(_DIR_PLUGIN_PDK.'classes/models/PdkRubriqueModel.php');

    
    
    $s = 'select * from spip_articles where id_article = '.$id_article;
    
    $res = sql_query($s);
    $all = sql_fetch_all($res);
    
    $id_rubrique = isset($all[0]['id_rubrique'])?$all[0]['id_rubrique']:'';
    
    $id_r = $id_rubrique;
    
    
    
    $rubrique_mdl = new PdkRubriqueModel();

      
    $rubriques = '';

    do { // get all parents id
        $rubrique = $rubrique_mdl->select_by_id($id_rubrique);
        $id_rubrique = $rubrique['id_parent'];
        $rubriques[] = $id_rubrique;
    } while ($id_rubrique != '0');

    
    
    $quantity = isset($cart['quantity'])?(int)$cart['quantity']:0;
    $price = isset($cart['price'])?$cart['price']:0;
    
    
    
    
    $sql = "SELECT * FROM `spip_ecatalogue_discount_signs` t 
         JOIN spip_ecatalogue_discount_rules r ON t.id_rule = r.id_rule

          WHERE
             '".date("Y-m-d H:i:s")."' between r.date_start and r.date_end and  
           (`t`.`object`='id_goods' and `t`.`value`='".$id_article."')";
    
    if ($id_r)
    {
        $sql.=" OR
           (`t`.`object`='id_rubrique' and `t`.`value`='".$id_r."') ";
    }
    
    if (is_array($rubriques))
    {
      foreach ($rubriques as $key=>$value)
      {
          if ($value!=0)
          {
              $sql.=" OR
           (`t`.`object`='id_rubrique' and `t`.`value`='".$value."') ";
          }
      }
    
    }
    
    if ($visiteur)
    {
       $sql.=" OR
           (`t`.`object`='visiteur' and `t`.`value`='".$visiteur."') ";
    
    }
    
    
    
    
    
    if ($quantity)
    {
        $sql.=" OR
           (`t`.`object`='quantity' and `t`.`value`='".$quantity."') ";
    }
    
    if ($price)
    {
        $sql.="OR
           (`t`.`object`='price' and `t`.`value`<='" . $price . "')";
    }
    
            
    $sql.= " LIMIT 1 ";
    
    //ppr($sql);
    
    
    //echo '<pre>';
    //print_r($sql);
    //echo '</pre>';
    
    
    
    
    $pre_res = sql_query($sql);
    $res_rule = sql_fetch_all($pre_res); 
    
    $action= isset($res_rule[0]['action'])?$res_rule[0]['action']:'';
    $value= isset($res_rule[0]['value'])?$res_rule[0]['value']:'';   
   
    return array('action'=>$action,'value'=>$value);

 }
 
 
 
 


function balise_EC_PRICE($p) {	
	return calculer_balise_dynamique($p,'EC_PRICE', array());
}

function balise_EC_PRICE_stat($args, $filtres) {    
	return $args;
}

function balise_EC_PRICE_dyn($id_article,$item_quantity = 1,$currency = 'UAH') {

//
    //$cart = pdk_load_class('Alohacart','classes');
    
    
    $product = pdk_load_class('ProductModel','classes');
    
    
    
                                              
    //$products = $cart->GetProducts(); 
	  $price_article = $product->GetProductPrice($id_article,$currency);
  
  /*$cart_quantity = 0;
  if (is_array($products))
  foreach ($products as $value)
  {
      $cart_quantity+=$value[1];
  }
  $cart_price = $cart->CountPrice();
  
  $cart = array('quantity'=>$cart_quantity,'price'=>$cart_price);
   
 
//Get info about user
	 $visiteur  = isset($GLOBALS["visiteur_session"]["statut"])?($GLOBALS["visiteur_session"]["statut"]):'';
  
  
  
   $action = get_discount_action($id_article,$cart,$visiteur);
  
   //ppr($id_article);
   //ppr($action);
   
    
   if (is_array($action)&&isset($action['action']))
   {
      switch ($action['action'])
      {
          case 'get_price':
// Getting price	    
			$id_group = isset($action['value'])?$action['value']:0;
			$sql_s = 'SELECT * from spip_ecatalogue_prices p where id_article='.$id_article.' and id_group = '.$id_group ;
			$res_s = sql_query($sql_s);
			
			//ppr($sql_s);
			
			$price_record = sql_fetch_all($res_s);
			
			//ppr($price_record);
			
			if (is_array($price_record)&&(!empty($price_record)))
			{
				$price_article = isset($price_record[0]['price'])?$price_record[0]['price']:0;	
			}
          
          break;
          case 'set_discount':
//	Set DISCOUNT
			$id_group = 1;
			$discount = isset($action['value'])?$action['value']:0;
			$sql_s = 'SELECT * from spip_ecatalogue_prices p where id_article='.$id_article.' and  id_group = '.$id_group ;
			$res_s = sql_query($sql_s);
			$price_record = sql_fetch_all($res_s);
			if (is_array($price_record)&&(!empty($price_record)))
			{
				$price_article = isset($price_record[0]['price'])?$price_record[0]['price']:0;	
                $price_article = $price_article - (($price_article/100)*$discount);  				
			}
	  
	  
	  
          break;
          default:
//  echo '<h1>Default</h1>'; 
			$id_group = 1;
			$sql_s = 'SELECT * from spip_ecatalogue_prices p where id_article='.$id_article.' and id_group = '.$id_group ;
			
			//ppr($sql_s);
			$res_s = sql_query($sql_s);
			$price_record = sql_fetch_all($res_s);
			//ppr($price_record);
			if (is_array($price_record)&&(!empty($price_record)))
			{
				$price_article = isset($price_record[0]['price'])?$price_record[0]['price']:0;	
			} 
	  
	  
          break;
          
   
      } 
   } 
    */
 
	  $price_article= $price_article * $item_quantity;
  


    return array('balise/ec_price', 0, 
		array('price' => $price_article, 'products'=> $products
    ));
}

?>